<template>
  <div class="app-container calendar-list-container">
    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row
              style="width: 100%">
      <el-table-column align="center" label="序号" width="65">
        <template slot-scope="scope">
          <span>{{scope.row.Id}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="告警信息">
        <template slot-scope="scope">
          <span>{{scope.row.Source}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="自愈项目">
        <template slot-scope="scope">
          <span>{{scope.row.Item}}</span>
        </template>
      </el-table-column>
      <el-table-column width="110px" align="center" label="自愈时间">
        <template slot-scope="scope">
          <span >{{scope.row.Duration}}</span>
        </template>
      </el-table-column>
      <el-table-column  width="200px" align="center" label="状态">
        <template slot-scope="scope">
          <span v-if="scope.row.Status == 0">未完成</span>
          <span v-else>已完成</span>
        </template>
      </el-table-column>
      <el-table-column  width="200px" align="center" label="自愈结果">
        <template slot-scope="scope">
          <span>{{scope.row.Result}}</span>
        </template>
      </el-table-column>
      <el-table-column   align="center" label="产生时间">
        <template slot-scope="scope">
          <span>{{scope.row.CeateTime.substring(0,10)}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="250" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <router-link :to="{name: 'taskDetail', params: {id: scope.row.Id}}" >
            <el-button type="primary" style="width:100px" size="mini">查看详情</el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>


  </div>
</template>

<script>
  import { getTaskList } from '@/api/recoverTask'
  import waves from '@/directive/waves' // 水波纹指令
  import { parseTime } from '@/utils'

  const calendarTypeOptions = [
    { key: 'HM', display_name: '手工录入' }
  ]

  // arr to obj ,such as { CN : "China", US : "USA" }
  const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
    acc[cur.key] = cur.display_name
    return acc
  }, {})

  export default {
    name: 'complexTable',
    directives: {
      waves
    },
    data() {
      return {
        tableKey: 0,
        list: null,
        delFormData: '',
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 10
        },
        calendarTypeOptions,
        temp: {
          scriptName: '',
          scriptFrom: 'HM',
          scriptContent: '',
          create: new Date(),
          endtIime: new Date()
        },
        dialogStatus: '',
        textMap: {
          update: '修改',
          create: '创建'
        },
        downloadLoading: false
      }
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'info',
          deleted: 'danger'
        }
        return statusMap[status]
      },
      typeFilter(type) {
        return calendarTypeKeyValue[type]
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true
        getTaskList(this.listQuery).then(response => {
          this.list = response.data.list
          this.total = response.data.total
          this.listLoading = false
        })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      handleSizeChange(val) {
        this.listQuery.limit = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getList()
      },
      resetTemp() {
        this.temp = {
          importance: 1,
          remark: '',
          create: new Date(),
          title: '',
          status: 'published',
          type: ''
        }
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => {
          if (j === 'timestamp') {
            return parseTime(v[j])
          } else {
            return v[j]
          }
        }))
      }
    }
  }
</script>
